package xin.clips.web.controller.gy;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.core.domain.Ztree;
import xin.clips.common.core.domain.entity.SysDept;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.GyKnowBaseMgt;
import xin.clips.system.domain.gy.GyKnowBaseType;
import xin.clips.system.service.gy.IGyKnowBaseMgtService;
import xin.clips.system.service.gy.IGyKnowBaseTypeService;

/**
 * 知识管理Controller
 *
 * @author yuanzy
 * @date 2023-03-02
 */
@Controller
@RequestMapping("/gy/knowMgt")
public class GyKnowBaseMgtController extends BaseController {
    private String prefix = "gy/knowMgt";

    @Autowired
    private IGyKnowBaseMgtService gyKnowBaseMgtService;

    @Autowired
    private IGyKnowBaseTypeService knowBaseTypeService;

    @RequiresPermissions("gy:knowMgt:view")
    @VisistLog(title = "知识管理")
    @GetMapping()
    public String knowMgt() {
        return prefix + "/knowMgt";
    }

    /**
     * 查询知识管理列表
     */
    @RequiresPermissions("gy:knowMgt:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyKnowBaseMgt gyKnowBaseMgt) {
        startPage();
        List<GyKnowBaseMgt> list = gyKnowBaseMgtService.selectGyKnowBaseMgtList(gyKnowBaseMgt);
        return getDataTable(list);
    }

    /**
     * 导出知识管理列表
     */
    @RequiresPermissions("gy:knowMgt:export")
    @Log(title = "知识管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyKnowBaseMgt gyKnowBaseMgt) {
        List<GyKnowBaseMgt> list = gyKnowBaseMgtService.selectGyKnowBaseMgtList(gyKnowBaseMgt);
        ExcelUtil<GyKnowBaseMgt> util = new ExcelUtil<GyKnowBaseMgt>(GyKnowBaseMgt.class);
        return util.exportExcel(list, "知识管理数据");
    }

    /**
     * 新增知识管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存知识管理
     */
    @RequiresPermissions("gy:knowMgt:add")
    @Log(title = "知识管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyKnowBaseMgt gyKnowBaseMgt) {
        if (!(gyKnowBaseMgtService.checkKnowNameUnique(gyKnowBaseMgt))) {
            return error("新增知识'" + gyKnowBaseMgt.getKnowName() + "'失败，该知识库类型下存在重名的知识名称");
        }
        return toAjax(gyKnowBaseMgtService.insertGyKnowBaseMgt(gyKnowBaseMgt));
    }

    /**
     * 修改知识管理
     */
    @RequiresPermissions("gy:knowMgt:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GyKnowBaseMgt gyKnowBaseMgt = gyKnowBaseMgtService.selectGyKnowBaseMgtById(id);
        mmap.put("gyKnowBaseMgt", gyKnowBaseMgt);
        return prefix + "/edit";
    }

    /**
     * 修改保存知识管理
     */
    @RequiresPermissions("gy:knowMgt:edit")
    @Log(title = "知识管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyKnowBaseMgt gyKnowBaseMgt) {
        if (!(gyKnowBaseMgtService.checkKnowNameUnique(gyKnowBaseMgt))) {
            return error("修改知识'" + gyKnowBaseMgt.getKnowName() + "'失败，该知识库类型下存在重名的知识名称");
        }
        return toAjax(gyKnowBaseMgtService.updateGyKnowBaseMgt(gyKnowBaseMgt));
    }

    /**
     * 删除知识管理
     */
    @RequiresPermissions("gy:knowMgt:remove")
    @Log(title = "知识管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gyKnowBaseMgtService.deleteGyKnowBaseMgtByIds(ids));
    }


    /**
     * 选择知识库树
     *
     * @param id
     */
    @RequiresPermissions("gy:knowMgt:list")
    @GetMapping("/selectKnowTypeTree/{id}")
    public String selectKnowTypeTree(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("knowBaseType", knowBaseTypeService.selectGyKnowBaseTypeById(id));
        return prefix + "/tree";
    }


    /**
     * 加载知识库列表树
     */
    @RequiresPermissions("gy:knowMgt:list")
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = knowBaseTypeService.selectKnowTypeTree(new GyKnowBaseType());
        return ztrees;
    }

}
